unit ActionButtonUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, MPlayer, StdCtrls, ComCtrls, Buttons, ExtCtrls;

type
  TActionButton = class(TFrame)
    mp: TMediaPlayer;
    selectSound: TSpeedButton;
    cooldownEdit: TEdit;
    HotKey2: THotKey;
    HotKey1: THotKey;
    Bevel1: TBevel;
    Timer1: TTimer;
    procedure cooldownEditChange(Sender: TObject);
  private
    FCooldownInterval: Integer;
    procedure SetFileName(const Value: String);
    function GetRight: Integer;
    procedure SetCooldownInterval(const Value: Integer);
    function GetFileName: String;
    { Private declarations }
  public
    { Public declarations }
    cooldownBegin: DWORD;
    cooldownPosition: Integer;

    procedure UpdateButton;

    property FileName: String read GetFileName write SetFileName;
    property CooldownInterval: Integer read FCooldownInterval write SetCooldownInterval;
    property Right: Integer read GetRight;

    constructor Create(AOwner: TComponent); override;
  end;

  TPlayQueue = class
  private
    FList: TList;
  public
    constructor Create;
    destructor Destroy; override;

    procedure Add(button: TActionButton);
    procedure Remove(button: TActionButton);
    procedure Update;
  end;

var
  PlayQueue: TPlayQueue;

implementation

{$R *.dfm}

uses KeyboardUnit;

procedure TActionButton.cooldownEditChange(Sender: TObject);
begin
  try
    cooldownInterval := StrToInt(cooldownEdit.Text);
  except
    cooldownInterval := 0;
  end;
end;

constructor TActionButton.Create(AOwner: TComponent);
begin
  inherited;

  Width := Bevel1.Width;
  Height := Bevel1.Height;
  cooldownInterval := 0;
  cooldownPosition := 0;
  cooldownBegin := 0;
end;

function TActionButton.GetFileName: String;
begin
  Result := mp.FileName;
end;

function TActionButton.GetRight: Integer;
begin
  Result := Left + Width;
end;

procedure TActionButton.SetCooldownInterval(const Value: Integer);
begin
  FCooldownInterval := Value;
  cooldownEdit.Text := IntToStr(Value);
end;

procedure TActionButton.SetFileName(const Value: String);
begin
  mp.FileName := Value;
  mp.Open;
  mp.Position := 0;
end;

procedure TActionButton.UpdateButton;
begin
  if cooldownBegin = 0 then begin
    if cooldownInterval > 0 then
      if IsShortCutDown(HotKey1.HotKey) or IsShortCutDown(HotKey2.HotKey) then begin
        PlayQueue.Remove(self);
        cooldownBegin := GetTickCount;
        cooldownPosition := 0;
      end;
  end else begin
    cooldownPosition := (GetTickCount - cooldownBegin) div 1000;
    if cooldownPosition >= cooldownInterval then begin
      cooldownPosition := 0;
      cooldownBegin := 0;
      PlayQueue.Add(self);
    end;
  end;
end;

{ TPlayQueue }

procedure TPlayQueue.Add(button: TActionButton);
begin
  FList.Add(button);
end;

constructor TPlayQueue.Create;
begin
  FList := TList.Create;
end;

destructor TPlayQueue.Destroy;
begin
  FList.Free;
  inherited;
end;

procedure TPlayQueue.Remove(button: TActionButton);
begin
  FList.Remove(button);
end;

procedure TPlayQueue.Update;
var
  button: TActionButton;
begin
  if FList.Count > 0 then begin
    button := TActionButton(FList[0]);
    if button.mp.Position = 0 then
      button.mp.Play
    else
    if button.mp.Position >= button.mp.Length then begin
      button.mp.Position := 0;
      FList.Delete(0);
    end;
  end;
end;

initialization
  PlayQueue := TPlayQueue.Create;
finalization
  PlayQueue.Free;
end.
